<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 主体卡片区域 -->
    <el-card v-loading="loading">
      <el-button type="primary" @click="showAddCateDialog">添加分类</el-button>
      <!-- 使用组件展示分类数据 -->
      <tree-table :data="catelist" :columns="columns" :selection-type="false" :expand-type="false" show-index
        index-text="#" border :show-row-hover="false">
        <!-- 是否有效模板 -->
        <template #isok="{row}">
          <i class="el-icon-success" v-if="!row.cat_deleted"></i>
          <i class="el-icon-error" v-else></i>
        </template>
        <!-- 操作栏模板 -->
        <template #operation>
          <el-button type="primary" size="mini" icon="el-icon-edit"></el-button>
          <el-button type="danger" size="mini" icon="el-icon-delete"></el-button>
          <el-button type="warning" size="mini" icon="el-icon-setting"></el-button>
        </template>
      </tree-table>

      <!-- 分页区域 -->

      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum" :page-sizes="[3, 5, 10, 15]" :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>

    </el-card>

    <!-- 添加分类对话框 -->
    <el-dialog title="添加分类" :visible.sync="addCateVisible" width="50%" @close="resetCateForm">

      <!-- 添加表单 -->
      <el-form ref="addCateFormRef" :model="addCateForm" :rules="addCateFormRules" label-width="100px">
        <el-form-item label="分类名称:" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类:">
          <!-- 级联 -->
          <el-cascader clearable v-model="selectedKeys" :options="parentCateList"
            :props="{ expandTrigger: 'hover',label:'cat_name',value:'cat_id',checkStrictly: true}"
            @change="handleChange"></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="addCateVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      selectedKeys: [],
      parentCateList: [],
      addCateVisible: false,
      // 添加表单数据对象
      addCateForm: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0
      },
      // 添加分类校验规则
      addCateFormRules: {
        cat_name: [{ required: true, message: '请输入分类名称', trigger: 'blur' }]
      },
      loading: false,
      queryInfo: {
        pagenum: 1,
        pagesize: 10
      },
      catelist: [],
      total: 0,
      columns: [
        { label: '分类名称', prop: 'cat_name' },
        { label: '是否有效', type: 'template', template: 'isok' },
        { label: '操作', type: 'template', template: 'operation' }
      ]

    }
  },
  methods: {
    saveCate () {
      this.$refs.addCateFormRef.validate(async boolean => {
        if (!boolean) return console.log('校验失败')
        const { data: res } = await this.$http.post('categories', this.addCateForm)
        if (res.meta.status !== 201) return this.$message.error('添加失败')
        this.addCateVisible = false
        this.getCateList()
      })
    },
    resetCateForm () {
      this.addCateForm.cat_pid = 0
      this.addCateForm.cat_level = 0
      this.addCateForm.cat_name = ''
      this.selectedKeys = []
    },
    // 获取父级分类值
    handleChange () {
      console.log(this.selectedKeys)
      if (this.selectedKeys.length === 0) {
        this.addCateForm.cat_pid = 0
        this.addCateForm.cat_level = 0
      } else {
        this.addCateForm.cat_level = this.selectedKeys.length
        this.addCateForm.cat_pid = this.selectedKeys[this.selectedKeys.length - 1]
      }
    },
    // 添加分类点击函数
    showAddCateDialog () {
      this.getParentCateList()
      this.addCateVisible = true
    },
    // 发送获取数据的axios
    async getParentCateList () {
      const { data: res } = await this.$http.get('categories', { params: { type: 2 } })
      this.parentCateList = res.data
      console.log(this.parentCateList)
    },
    handleSizeChange (size) {
      this.queryInfo.pagesize = size
      this.getCateList()
    },
    handleCurrentChange (page) {
      this.queryInfo.pagenum = page
      this.getCateList()
    },
    async getCateList () {
      this.loading = true
      const { data: res } = await this.$http.get('categories', { params: this.queryInfo })
      if (res.meta.status !== 200) return this.$message.errror('请求失败')
      this.$message.success('请求成功')
      this.catelist = res.data.result
      this.total = res.data.total
      // console.log(this.catelist)
      this.loading = false
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: {},
  created () {
    this.getCateList()
  },
  mounted () { }
}
</script>

<style scoped lang='less'>
</style>
